package com.crazy.crazyuserservice.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.common.ResultUtils;
import com.crazy.crazycommon.exception.BusinessException;
import com.crazy.crazymodel.model.dto.admin.AdminLoginRequest;

import com.crazy.crazymodel.model.vo.LoginAdminVO;

import com.crazy.crazyuserservice.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {
    @Resource
    private AdminService adminService;

    /**
     * 操作员登录
     * @param adminLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<LoginAdminVO> adminLogin(@RequestBody AdminLoginRequest adminLoginRequest, HttpServletRequest request) {
        log.info("adminLoginRequest:{}", adminLoginRequest);
        if (adminLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String adminAccount = adminLoginRequest.getAdminAccount();
        String adminPassword = adminLoginRequest.getAdminPassword();
        if (StringUtils.isAnyBlank(adminAccount, adminPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LoginAdminVO loginAdminVO = adminService.adminLogin(adminAccount, adminPassword, request);
        return ResultUtils.success(loginAdminVO);
    }

}
